<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class User_model extends CI_Model{

	public function __construct(){
		parent::__construct();
		$this->load->library('ion_auth');
		$this->load->database();
	}	
		public function register($username, $password, $email, $additional_data = false, $group_id,$building_id)
	{
		$this->trigger_events('pre_register');
		
	    if ($this->identity_column == 'email' && $this->email_check($email))
	    {
			$this->ion_auth->set_error('account_creation_duplicate_email');
			return FALSE;
	    }
	    elseif ($this->identity_column == 'username' && $this->username_check($username))
	    {
			$this->ion_auth->set_error('account_creation_duplicate_username');
			return FALSE;
	    }

	    // If username is taken, use username1 or username2, etc.
	    if ($this->identity_column != 'username')
	    {
			for($i = 0; $this->username_check($username); $i++)
			{
				if($i > 0)
				{
					$username .= $i;
				}
			}
	    }


	    // IP Address
	 //   $ip_address = $this->input->ip_address();
	    $salt       = $this->store_salt ? $this->salt() : FALSE;
	    $password	= $this->hash_password($password, $salt);

	    // Users table.
	    $data = array(
			'username'   => $username,
			'password'   => $password,
			'email'      => $email,
			'building_id' => $building_id,
			'created_on' => now(),
			'last_login' => now(),
			'active'     => 1
		);

	    if ($this->store_salt)
	    {
			$data['salt'] = $salt;
	    }

		//filter out any data passed that doesnt have a matching column in the users table
		//and merge the set user data and the additional data
		$user_data = array_merge($this->_filter_data('users', $additional_data), $data);
	    
	    $this->trigger_events('extra_set');

	    $this->db->insert($this->tables['users'], $user_data);

	    $id = $this->db->insert_id();

		$this->add_to_group($group_id, $id);
			


		$this->trigger_events('post_register');
		
	    return (isset($id)) ? $id : FALSE;
	}

	public function registerXibo($data){
		$this->db->insert($this->name,$data);
	}
	function checkExistence($fieldname,$value){
        $sql = "SELECT * FROM ".$this->name." WHERE $fieldname = '$value'";
        $query=$this->db->query($sql);
        if ($query->num_rows() >0){
            return true;
        }
        return false;
    }
	
	function getRoleId($id){
		$this->load->database();
        $sql = "SELECT role_id FROM users_groups WHERE user_id = '$id'";
        $query=$this->db->query($sql);
		$results=$query->result();
        $role="";
		foreach($results as $result):
				$role=$result->role_id;
        endforeach;
        return $role;
    }
	
	function getChildId($id){
		$this->load->database();
        $sql = "SELECT a.username,a.password,a.id 
				FROM users a, users_groups b
				WHERE a.id = b.user_id
				AND b.group_id
				IN(
				SELECT id
				FROM  `groups` 
				WHERE root ='$id'
				)";
        $query=$this->db->query($sql);
		$results=$query->result();
		$res="";
		foreach($results as $result):
				$res.="<option id='$result->username' class='$result->password' value='$result->username'>$result->username</option>";
        endforeach;
        return $res;
    }
	function getGroupId($id){
        $sql = "SELECT group_id FROM users_groups WHERE user_id = '$id'";
        $query=$this->db->query($sql);
		$results=$query->result();
        $group="";
		foreach($results as $result):
				$group=$result->group_id;
        endforeach;
        return $group;
    }
	function register_xibo($data){
		$dsn1 ='mysql://root:1234@localhost/xibo?char_set=utf8&dbcollat=utf8_general_ci&autoinit=false&db_debug=true&p_connect=true&cache_on=false&stricton=false';
		$this->dbx = $this->load->database($dsn1,TRUE);
		$this->dbx->insert('user',$data);
		$lkdata=array("GroupId"=>3,"UserId"=>$this->dbx->insert_id());
		$this->dbx->insert('lkusergroup',$lkdata);
	}
	function registerGroup($user_id,$group,$root=0){
		$this->load->database();
		$this->getLastGroupId();
		if($group=='Customer'){
			$this->dbx->insert('lkusergroup',$lkdata);
		}
	}
	 function getLastGroupId(){
        $sql = "SELECT id as result FROM group ORDER BY id desc";
        $query=$this->db->query($sql);
        foreach($query->result() as $result){
            return $result->result;
        }
        $this->db->close();
        return "none";
    }
    function getValue($fieldname,$cond){
        $sql = "SELECT ".$fieldname." as result FROM ".$this->name." WHERE ".$cond;
        $query=$this->db->query($sql);
        foreach($query->result() as $result){
            return $result->result;
        }
        $this->db->close();
        return "none";
    }
}